using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using TCDbService;
using TCEntity;

namespace TimeCameraServer.Controllers
{
  [Route("[controller]")]
  public class ImageController : FailureController
  {
    readonly ImageService _service;
    public ImageController(ImageService service) => _service = service;
    [HttpPost("upload")]
    public async ValueTask<IActionResult> UploadAsync((User user, byte[] picture) message) => await FailureProcess(async () =>
      {
        await _service.UploadAsync(message.user, message.picture);
        return NoContent();
      });
    [HttpGet("download/user")]
    public async ValueTask<IActionResult> DownloadAsync(User user) => await FailureProcess(async () =>
    {
      var images = await _service.DownloadAsync(user);
      return Ok(images);
    });
    [HttpGet("download/date")]
    public async ValueTask<IActionResult> DownloadAsync((User user, DateTime date) message) => await FailureProcess(async () =>
     {
       var images = await _service.DownloadAsync(message.user, message.date);
       return Ok(images);
     });
    [HttpGet("download/id")]
    public async ValueTask<IActionResult> DownloadAsync((User user, int id) message) => await FailureProcess(async () =>
    {
      var image = await _service.DownloadAsync(message.user, message.id);
      return Ok(image);
    });
    [HttpDelete("delete")]
    public async ValueTask<IActionResult> DeleteAsync((User user, int id) message) => await FailureProcess(async () =>
    {
      await _service.DeleteAsync(message.user, message.id);
      return NoContent();
    });
    [HttpGet("filter")]
    public async ValueTask<IActionResult> FilterAsync(int[] order) => await FailureProcess(async () =>
    {
      var images = await _service.FilterAsync(order);
      return Ok(images);
    });
  }
}
